package com.sskj.hangqing.ui.activity;


import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.hangqing.R;
import com.sskj.hangqing.R2;
import com.sskj.hangqing.bean.GuideListBean;
import com.sskj.hangqing.presenter.GuideWebActivityPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.zzhoujay.richtext.RichText;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = RConfig.APP_GUIDE_WEB)
public class GuideWebActivity extends BaseActivity<GuideWebActivityPresenter> {
    @BindView(R2.id.tvContent)
    TextView tvContent;

    @Autowired
    String content;
    @Autowired
    String time;
    @Autowired
    String title;
    @Autowired
    String id;
    @Autowired
    String url = "";
    @Autowired
    String head;
    @Autowired
    String xieyi;
    @Autowired
    boolean isBorrow = false;
    @BindView(R2.id.tvWebTitle)
    TextView tvWebTitle;
    @BindView(R2.id.tvTime)
    TextView tvTime;


    @Override
    protected int getLayoutId() {
        return R.layout.app_activity_guide_web;
    }

    @Override
    public GuideWebActivityPresenter getPresenter() {
        return new GuideWebActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        setTitle(head);

        if (!TextUtils.isEmpty(url)) {
            if (!url.contains("http")) {
                url = "http://" + url;
            }
        }
        if (TextUtils.isEmpty(url) && !TextUtils.isEmpty(content)) {
            updateUI();
        }
        if (!TextUtils.isEmpty(id)) {
            mPresenter.getContent(id);
        }
        if (!TextUtils.isEmpty(xieyi)) {
            setTitle(App.INSTANCE.getString(R.string.hang_guideWebActivity1_j));
            mPresenter.getXieyi(xieyi);
        }
        if (isBorrow) {
            setTitle(App.INSTANCE.getString(R.string.hang_guideWebActivity2_j));

            mPresenter.borrow();
        }

    }

    public void updateUI() {
        if (TextUtils.isEmpty(content)) {
            return;
        }

        RichText.from(content).into(tvContent);
    }


    public void getContentSuccess(GuideListBean data) {
        content = data.getContent();
        time = data.getCreate_time();
        title = data.getTitle();
        tvWebTitle.setText(title);
        tvTime.setText(time);
        updateUI();
    }

    public void updateContent(String data) {
        this.content = data;
        updateUI();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
